/*
 * Board.h
 *
 *  Created on: 12.10.2010
 *      Author: jilly
 */

#ifndef BOARD_H_
#define BOARD_H_

#include <list>
#include "Move.h"
#include "Piece.h"

using namespace std;

/**
 * represents chess-board
 */
class Board
{
private:
	/** size of board, board is square size x size */
	unsigned int size;
	/** number of pieces on board excluding queen and bishop */
	unsigned int numberOfPieces;
	/** array representing board and positions of pieces excluding position of queen and bishop */
	bool** board;
	/** represents queen, its actual position */
	Piece* queen;
	/** represents bishop, its actual position */
	Piece* bishop;
	/** dynamically allocates board based on size */
	void alloc(unsigned int);
	bool boardArrayAllocated;
	/** frees dynamically allocated memory */
	void dealloc();
public:
	Board(unsigned int size);
	~Board();

	/** returns size of chess-board */
	unsigned int getSize();
	bool** getBoard();
	/** returns number of pieces on board (excluding queen and bishop) */
	unsigned int getNumberOfPieces();
	/** checks if piece is on position [x,y] (excluding queen and bishop) */
	bool isPieceOnPosition(unsigned int x, unsigned int y);
	/**
	 * removes piece from position [x,y]
	 * (excluding queen and bishop)
	 * decrements count of pieces
	 */
	void removePiece(unsigned int x, unsigned int y);
	/** adds piece to position [x,y], increments count of pieces
	 * returns this (for method chaining)
	 *
	 * throws BoardException
	 * */
	Board* addPiece(unsigned int x, unsigned int y);
	/** returns queen piece */
	Piece* getQueen();
	/** returns bishop piece */
	Piece* getBishop();
	/** places queen on position [x,y] */
	void placeQueen(unsigned int x, unsigned int y);
	/** place bishop on position [x,y] */
	void placeBishop(unsigned int x, unsigned int y);
	/**
	 * moves queen to position [x,y], if there's piece on this position
	 * it is automatically conquered
	 * returns if move hits piece or not
	 */
	bool moveQueen(unsigned int x, unsigned int y);
	/**
	 * moves bishop to position [x,y], if there's piece on this position
	 * it is automatically conquered
	 * returns if move hits piece or not
	 */
	bool moveBishop(unsigned int x, unsigned int y);
	/**
	 * returns list of all possible moves
	 * for queen. Moves are based on chess rules
	 * and also reflect constraint that queen
	 * can't conquer bishop
	 */
	list<Move*> getQueenPossibleMoves();
	/**
	 * returns list of all possible moves
	 * for bishop. Moves are based on chess rules
	 * and also reflect constraint that bishop
	 * can't conquer queen
	 */
	list<Move*> getBishopPossibleMoves();
	void printBoard();
	void writeBoard(ofstream& streamRef);
};

#endif /* BOARD_H_ */
